﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreEx.Database.Metadata;

namespace CoreEx.Database.MetaData.Implementation
{
    [Serializable]
    public class TableInfo : ITableInfo
    {
        private readonly IDictionary<string, IColumnInfo> _columns = 
            new Dictionary<string, IColumnInfo>();
        private readonly IList<IRelationInfo> relations 
            = new List<IRelationInfo>();
        private IList<IForeignKeyInfo> _foreignKeys = new List<IForeignKeyInfo>();


        public string TableName { get; set; }        
        public string TableSchema { get; set; }
        public string TableCatalog { get; set;}
        

        public IPrimaryKeyInfo PrimaryKey { get; set; }
        
        public IList<IRelationInfo> Relations
        {
            get { return relations; }           
        }

        public IList<IForeignKeyInfo> ForeignKeys
        {
            get { return _foreignKeys; }

        }


        public IDictionary<string, IColumnInfo> Columns
        {
            get { return _columns; }           
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(TableCatalog))
                sb.AppendFormat("{0}.", TableCatalog);
            if (!string.IsNullOrEmpty(TableSchema))
                sb.AppendFormat("{0}.", TableSchema);
            if (!string.IsNullOrEmpty(TableName))
                sb.AppendFormat("{0}", TableName);
            if (sb.Length > 0)
                return sb.ToString();
            
            return base.ToString();
        }
    }
}
